var UIPanel_Main = (function () {
    function UIPanel_Main() {
        this._view = fairygui.UIPackage.createObject("Package", "mainScene").asCom;
        if (window._view) {
            this._view.setSize(window._view.width, window._view.height);
            this._view.addRelation(window._view, fairygui.RelationType.Size);
            window._view.addChild(this._view);
            window.mainPanel = this;
        } else {
            this._view.setSize(fairygui.GRoot.inst.width, fairygui.GRoot.inst.height);
            fairygui.GRoot.inst.addChild(this._view);
            window._view = this._view;
            window.mainPanel = this;
        }

        this.onUIElementInit();

        Observer
    }

    var __proto = UIPanel_Main.prototype;
    var onlineNum = 0;
    var gameType = null;

    __proto.onUIElementInit = function () {
        this._view.getChild("bgLoader").url = "res/login.jpg";
        this.panel = this._view.getChild("panel")
        this.frameTop = this.panel.getChild("frameTop");
        this.frameCenter = this.panel.getChild("frameCenter");
        this.frameBottom = this.panel.getChild("frameBottom");
        this.gaizi = this.frameCenter.getChild('gaizi');

        this.group5 = this.frameCenter.getChild('group5');
        this.group5.visible = false;
        this.btnCancel = this.frameCenter.getChild('btnCancel');
        this.btnCancel.onClick(this, function () {
            if (this.btnCancel.selected) {
                Tip.instance().show('请点击需要撤回的区域');
            }
        })

        var children = this.frameCenter._children;
        // console.log("top", this.frameTop.width, this.frameTop.height)
        // console.log("center", this.frameCenter.width, this.frameCenter.height)
        // console.log("bottom", this.frameBottom.width, this.frameBottom.height)


        for (var i in children) {
            var c = children[i];
            if (c.name.indexOf("record") >= 0 || c.name.indexOf("block") >= 0 || c.name.indexOf("btn") >= 0) {
                ///console.log(c.name, c.width, c.height)
                c.on(Event.MOUSE_DOWN, this, this.mouseDown);
                c.on(Event.MOUSE_UP, this, this.mouseUp);
            }
        }
        for (var i = 0; i < 3; i++) {
            var item = this.gaizi.getChild('touzi' + i);
            var url = fairygui.UIPackage.getItemURL('Package', "开奖筛" + (i + 1));
            item.url = url;

        }
        //ui://yqvt9oa8rp7dp
        for (var i = 0; i < 6; i++) {
            var item = this.frameCenter.getChild("block1_" + i);
            var url = fairygui.UIPackage.getItemURL('Package', "bg-0" + (i + 1));
            item.icon = url;
            item.getController('cc_light').selectedIndex = 0;
            item.data = [i + 1]

        }

        var m = 0;

        for (var i = 5; i >= 1; i--) {
            for (var n = 0; n < i; n++) {
                var item = this.frameCenter.getChild("block2_" + m);
                item.getController('cc_light').selectedIndex = 0;
                m++;
                item.getChild('icon').url = fairygui.UIPackage.getItemURL('Package', "bg-0" + (6 - i));
                item.getChild('url1').url = fairygui.UIPackage.getItemURL('Package', "bg-0" + (6 - i + 1 + n));
                item.data = [6 - i, 6 - i + 1 + n];
            }
        }

        var n1 = 1;
        var n2 = 2;
        var n3 = 3;
        for (var i = 0; i < 20; i++) {
            var item = this.frameCenter.getChild("block3_" + i);
            item.getController('cc_light').selectedIndex = 0;
            item.getChild('icon0').url = fairygui.UIPackage.getItemURL('Package', "bg-0" + n1);
            item.getChild('icon1').url = fairygui.UIPackage.getItemURL('Package', "bg-0" + n2);
            item.getChild('icon2').url = fairygui.UIPackage.getItemURL('Package', "bg-0" + n3);
            item.data = [n1, n2, n3];

            n3++;
            if (n3 > 6) {
                n2++;
                n3 = n2 + 1;
                if (n2 > 5) {
                    n1++;
                    n2 = n1 + 1;
                    n3 = n2 + 1;
                }
            }

        }
        this.betArr = new Array();

        for (var i = 0; i < 6; i++) {
            var btn = this.frameBottom.getChild("bet" + i);
            btn.index = 0;
            btn.on(Event.MOUSE_UP, this, this.mouseUpBet);
            this.betArr.push(btn)
        }
        this.curBet = 10;

        this.initSonPanel();
        this.initBottomBtn();
        this.showKaijiang([0, 0, 0], false);

        this.registeredEvent();
    }
    __proto.initSonPanel = function () {
        this.rechargePanel = new UIPanel_Recharge();
        this.tiXianPanel = new UIPanel_TiXian();
        this.tuiguangPanel = new UIPanel_Tuiguang();
        this.betRecordPanel = new UIPanel_BetRecord();
        this.myPanel = new UIPanel_My();

        this.rechargePanel.panel = this._view.getChild("recharge");
        this.tiXianPanel.panel = this._view.getChild("cash");
        this.betRecordPanel.panel = this._view.getChild("record");
        this.tuiguangPanel.panel = this._view.getChild("popularize");
        this.myPanel.panel = this._view.getChild("myInfo");

        this.rechargePanel.init();
        this.tiXianPanel.init();
        this.betRecordPanel.init();
        this.tuiguangPanel.init();
        this.myPanel.init();
    }
    __proto.initBottomBtn = function () {
        var btnRecharge = this.frameBottom.getChild("btnRecharge");
        var btnTixian = this.frameBottom.getChild("btnTixian");
        var btnBetRecord = this.frameBottom.getChild("btnBetRecord");
        var btnTuiguang = this.frameBottom.getChild("btnTuiguang");
        var btnMy = this.frameBottom.getChild("btnMy");

        btnRecharge.on(Event.MOUSE_UP, this, function () {
            console.log('btnRecharge');
            this.rechargePanel.panel.visible = true;

        });
        btnTixian.on(Event.MOUSE_UP, this, function () {
            console.log('btnTixian');
            this.tiXianPanel.panel.visible = true;
        });
        btnBetRecord.on(Event.MOUSE_UP, this, function () {
            console.log('btnBetRecord');
            this.betRecordPanel.panel.visible = true;
            this.betRecordPanel.getData(2, 2, 1);
        });
        btnTuiguang.on(Event.MOUSE_UP, this, function () {
            console.log('btnTuiguang');
            this.tuiguangPanel.panel.visible = true;

        });
        btnMy.on(Event.MOUSE_UP, this, function () {
            console.log('btnMy');
            this.myPanel.panel.visible = true;
        });
        var showBetRecord = this.betRecordPanel.panel;
        Laya.stage.on('showBetRecord', this, function (args) {
            showBetRecord.visible = true;
        })
    }
    __proto.showKaijiang = function (list, show) {
        var ctrl = this.gaizi.getController('cc_kaijiang')
        if (show) ctrl.selectedIndex = 0;
        else ctrl.selectedIndex = 1;
        for (var i = 0; i < 3; i++) {

            var item = this.gaizi.getChild('touzi' + i);
            var url = fairygui.UIPackage.getItemURL('Package', "开奖筛" + (list[i]));
            item.url = url;
        }

    }
    __proto.mouseDown = function (e) {
        //console.log('mouseDown')
        var item = e.target.$owner;
        //ctr_btn.selectedIndex = 1
    }
    __proto.mouseUpBet = function (e) {
        var item = e.target.$owner;
        var bet = item.title;
        if (bet == '1千') {
            bet = 1000;
        }
        this.curBet = bet;

        console.log("bet up ", item.name, bet)
    }
    __proto.mouseUp = function (e) {
        //console.log('mouseDown')
        var item = e.target.$owner;
        console.log(item.name, item.width, item.height)
        var name = item.name;
        if (name.indexOf("block") >= 0) {
            name = name.replace('block', '')
            var arr = name.split('_')
            //console.log("block ",arr[0],arr[1])
            this.onChooseBlock(item, arr[0], arr[1]);
        }
        if (name === 'btnOnline') {
            console.log("在线人数");
            item.title = onlineNum;
            return;
        }
        if (!isBet) {
            this.group5.visible = false;
            this.btnCancel.selected = false;
            Tip.instance().show('开奖中');
            return;
        }

        if (name === 'btn_3Times') {
            console.log("3倍");
            Observer.fire('startBetSanZhong', { msg: 3 })
            this.group5.visible = false;
        }
        if (name === 'btn_6Times') {
            console.log("6倍");
            Observer.fire('startBetSanZhong', { msg: 6 })
            this.group5.visible = false;

        }
        if (name === 'btn_9Times') {
            console.log("9倍");
            Observer.fire('startBetSanZhong', { msg: 9 })
            this.group5.visible = false;
        }
        if (name === 'btn_cancel') {
            console.log("取消");
            this.group5.visible = false;
        }
        //console.log("button up ",item.name)

        //ctr_btn.selectedIndex = 1
    }
    __proto.addBet2Item = function (item) {
        var posNode = item.getChild('betPos')
        var children = posNode._children;
        var num = children.length;
        if (num >= 6) return;
        //10 50  100  300 500  1000
        var betMap = { 10: 1, 50: 2, 100: 3, 300: 4, 500: 5, 1000: 6 }
        var h = posNode.height;
        var bet = fairygui.UIPackage.createObject('Package', 'btnBet' + betMap[this.curBet]);
        var scale = h / bet.height;
        bet.scaleX = scale;
        bet.scaleY = scale;
        bet.x = num * bet.width * scale;
        //posNode.width = num * bet.width * scale;
        //bet.pivotX=0.5;
        //bet.pivotY=0.5;
        posNode.addChild(bet);

    }
    __proto.removeBet2Item = function (item) {
        var posNode = item.getChild('betPos')
        posNode.removeChildren();
    }
    __proto.onChooseBlock = function (item, type, n) {
        console.log('block', type, n, item.data);

        if (this.btnCancel.selected) {
            console.log("撤回");
            if (item.getChild("txt_self").text === 0) {
                Tip.instance().show('没有可撤回的筹码!');
                return;
            }
            if (type == 'Big') {
                Observer.fire('cancelBet', { bet: 0, betType: 1, betChoose: 0 })
            }
            if (type == 'Small') {
                Observer.fire('cancelBet', { bet: 0, betType: 2, betChoose: 0 })
            }
            if (type == 'Odd') {
                Observer.fire('cancelBet', { bet: 0, betType: 3, betChoose: 0 })
            }
            if (type == 'Even') {
                Observer.fire('cancelBet', { bet: 0, betType: 4, betChoose: 0 })
            }
            if (type == 'Sanquan') {
                Observer.fire('cancelBet', { bet: 0, betType: 5, betChoose: 0 })
            }
            if (type == 1) {
                Observer.fire('cancelBet', { bet: 0, betType: 6, betChoose: n })
            }
            if (type == 2) {
                Observer.fire('cancelBet', { bet: 0, betType: 7, betChoose: n })
            }
            if (type == 3) {
                Observer.fire('cancelBet', { bet: 0, betType: 8, betChoose: n })
            }
            this.removeBet2Item(item);
            return;
        }

        var count = 0;
        for (var i = 0; i < 6; i++) {
            if (this.betArr[i].selected) {
                count = this.betArr[i].title;
                if (this.betArr[i].title == '1千') {
                    count = 1000;
                }
            }
        }
        if (count === 0) {
            console.log('没选筹码');
            Tip.instance().show('请选择筹码！');
            return;
        }
        var posNode = item.getChild('betPos');
        if (posNode) {
            //console.log('betPos', posNode.width, posNode.height)
            this.addBet2Item(item);
        }
        if (type == 'Big') {
            Observer.fire('startBet', { bet: count, betType: 1, betChoose: 0 })
            return;
        }
        if (type == 'Small') {
            Observer.fire('startBet', { bet: count, betType: 2, betChoose: 0 })
            return;
        }
        if (type == 'Odd') {
            Observer.fire('startBet', { bet: count, betType: 3, betChoose: 0 })
            return;
        }
        if (type == 'Even') {
            Observer.fire('startBet', { bet: count, betType: 4, betChoose: 0 })
            return;
        }
        if (type == 'Sanquan') {
            Observer.fire('startBet', { bet: count, betType: 5, betChoose: 0 })
            return;
        }
        if (type == 1) {
            Observer.fire('startBet', { bet: count, betType: 6, betChoose: n })
            return;
        }
        if (type == 2) {
            Observer.fire('startBet', { bet: count, betType: 7, betChoose: n })
            return;
        }
        if (type == 3) {
            this.group5.visible = true;
            Observer.regist('startBetSanZhong', function (args) {
                Observer.fire('startBet', { bet: count * args.msg, betType: 8, betChoose: n })
            })
            return;
        }

    }
    __proto.onClickBtnGetVCode = function () {
        console.log("发送验证码");

        if (isBlank(this.text_Tel.text)) {
            alert("手机号不能为空");
            return;
        }
        this.getVCode(this.text_Tel.text).then(function (res) {
            var data = res.data;
            if (data.code == 200) {
                alert("获取成功！！");
                console.log("获取成功")
            }
            else {
                alert(data.msg);
            }
        }).catch(function (err) {
            alert("系统异常, 请稍后再试");
        });
    }
    __proto.onClickBtnRegister = function () {
        console.log("立即参加");

        if (isBlank(this.text_Tel.text)) {
            alert("手机号不能为空");
            return;
        }
        if (isBlank(this.text_VCode.text)) {
            alert("验证码不能为空");
            return;
        }
        this.register(this.text_Tel.text, this.text_VCode.text).then(function (res) {
            var data = res.data;
            if (data.code == 200) {
                alert("登记成功！！");
                console.log("加入成功");
            }
            else {
                alert(data.msg);
            }
        }).catch(function (err) {
            alert("系统异常, 请稍后再试");
        });
    }
    __proto.getVCode = function (tel) {
        return axios.post(appConfig.webAddr + "/acti/code/get", {
            mobile: tel,
        });
    };
    __proto.register = function (tel, vCode) {
        return axios.post(appConfig.webAddr + "/acti/signin", {
            mobile: tel,
            code: vCode
        });
    };

    __proto.gameWinner = function (v) {
        var txt = this.frameTop.getChild("txt_res");
        txt.visible = true;
        var trans = this.frameTop.getTransition("t0");
        txt.text = v > 0 ? '+' + v : v;
        trans.play();
    }
    __proto.gameType = function (v) {
        txt = this.frameTop.getChild("txt_gameType");
        txt.text = v;
    }
    __proto.gameState = function (v) {
        var txt = this.frameTop.getChild("txt_gameState");
        txt.text = v;
    }
    __proto.setYaDa = function (v_all, v_self) {
        var block = this.frameCenter.getChild("blockBig");
        var txt_all = block.getChild("txt_all");
        var txt_self = block.getChild("txt_self");

        txt_all.text = v_all;
        if (v_self != null) {
            txt_self.text = v_self;
        }
    }
    __proto.setYaXiao = function (v_all, v_self) {
        var block = this.frameCenter.getChild("blockSmall");
        var txt_all = block.getChild("txt_all");
        var txt_self = block.getChild("txt_self");

        txt_all.text = v_all;
        if (v_self != null) {
            txt_self.text = v_self;
        }
    }
    __proto.setYaDan = function (v_all, v_self) {
        var block = this.frameCenter.getChild("blockOdd");
        var txt_all = block.getChild("txt_all");
        var txt_self = block.getChild("txt_self");

        txt_all.text = v_all;
        if (v_self != null) {
            txt_self.text = v_self;
        }

    }
    __proto.setYaShuan = function (v_all, v_self) {
        var block = this.frameCenter.getChild("blockEven");
        var txt_all = block.getChild("txt_all");
        var txt_self = block.getChild("txt_self");

        txt_all.text = v_all;
        if (v_self != null) {
            txt_self.text = v_self;
        }
    }
    __proto.setYaSanQuan = function (v_all, v_self) {
        var block = this.frameCenter.getChild("blockSanquan");
        var txt_all = block.getChild("txt_all");
        var txt_self = block.getChild("txt_self");

        txt_all.text = v_all;
        if (v_self != null) {
            txt_self.text = v_self;
        }
    }
    __proto.setYaDanZong = function (v_all, v_self) {
        for (var i = 0; i < 6; i++) {
            var block = this.frameCenter.getChild("block1_" + i);
            var txt_all = block.getChild("txt_all");
            var txt_self = block.getChild("txt_self");

            txt_all.text = v_all[i];

            if (v_self) {
                txt_self.text = v_self[i];
            }
        }
    }
    __proto.setYaShuanZong = function (v_all, v_self) {
        for (var i = 0; i < 15; i++) {
            var block = this.frameCenter.getChild("block2_" + i);
            var txt_all = block.getChild("txt_all");
            var txt_self = block.getChild("txt_self");
            txt_all.text = v_all[i];
            if (v_self) {
                txt_self.text = v_self[i];
            }
        }
    }
    __proto.setYaSanZong = function (v_all, v_self) {
        for (var i = 0; i < 20; i++) {
            var block = this.frameCenter.getChild("block3_" + i);
            var txt_all = block.getChild("txt_all");
            var txt_self = block.getChild("txt_self");

            txt_all.text = v_all[i];
            if (v_self) {
                txt_self.text = v_self[i];
            }
        }

    }

    var delaytimer = null;
    __proto.countdown = function (v) {
        var txt_time = this.frameTop.getChild("txt_time");
        if (delaytimer) {
            clearInterval(delaytimer);
        }
        delaytimer = setInterval(function () {
            v--;
            txt_time.text = v + ' s';
            if (v <= 0) {
                clearInterval(delaytimer);
            }
        }.bind(this), 1000);
    }
    __proto.registeredEvent = function () {
        var self = this;
        Observer.regist('initUserData', function (args) {
            self.frameTop.getChild("txt_userName").text = args.msg.self.name;
            self.frameTop.getChild("txt_uid").text = args.msg.self.uid;
            self.frameTop.getChild("userIcon").icon = args.msg.self.headimgurl;
            self.frameTop.getChild("txt_coinNum").text = args.msg.self.coinNum;
            userInfo.setUid(args.msg.self.uid);
            userInfo.setSeatId(args.msg.self.seatId);
        })
        Observer.regist('updateData', function (args) {
            self.countdown(args.msg.leftTime);
            if (args.msg.self) {
                self.frameTop.getChild("txt_coinNum").text = args.msg.self.coinNum;
            }
            onlineNum = args.msg.onlineNum;
            self.setYaDa(args.msg.da, args.self.da);
            self.setYaXiao(args.msg.xiao, args.self.xiao);
            self.setYaDan(args.msg.dan, args.self.dan);
            self.setYaShuan(args.msg.suan, args.self.suan);
            self.setYaSanQuan(args.msg.sanQuan, args.self.sanQuan);
            self.setYaDanZong(args.msg.danZong, args.self.danZong);
            self.setYaShuanZong(args.msg.suanZongArea, args.self.suanZongArea);
            self.setYaSanZong(args.msg.sanZongArea, args.self.sanZongArea);
        })
        Observer.regist('updateDataNoSelf', function (args) {
            self.setYaDa(args.msg.da);
            self.setYaXiao(args.msg.xiao);
            self.setYaDan(args.msg.dan);
            self.setYaShuan(args.msg.suan);
            self.setYaSanQuan(args.msg.sanQuan);
            self.setYaDanZong(args.msg.danZong);
            self.setYaShuanZong(args.msg.suanZongArea);
            self.setYaSanZong(args.msg.sanZongArea);
        })
        Observer.regist('updateGameType', function (args) {
            self.gameType(args.msg);
            gameType = args.msg;
        })
        Observer.regist('updateGameState', function (args) {
            self.gameState(args.msg)
        })
        Observer.regist('updateGameWinner', function (args) {
            self.gameWinner(args.msg)
        })
        Observer.regist('updateKaijiang', function (args) {
            self.showKaijiang(args.msg, true);
        })

    }



    function isBlank(str) {
        return (!str || /^\s*$/.test(str));
    }

    return UIPanel_Main;
}())



